package dao;


import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.*;
import pojo.Dept;
import pojo.Employee;

import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MybatisTest {
    Reader reader = null;
    SqlSession sqlSession = null;

    @BeforeEach
    public void open() {
        try {
            reader = Resources.getResourceAsReader("MybatisConfig.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
            sqlSession = sqlSessionFactory.openSession();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @AfterEach
    public void close() {
        sqlSession.close();
        try {
            reader.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void addDept() {
        try {
            Dept dept = new Dept();
            dept.setName("售前部");
            sqlSession.insert("DEPT.insertDept",dept);
            System.out.println(dept.getId());
            sqlSession.commit();
        } catch (Exception e) {
            sqlSession.rollback();
            throw new RuntimeException(e);
        }
    }

    @Test
    public void addEmployee() {
        try {
            Employee employee = new Employee();
            employee.setHiredate(new Date());
            employee.setSalary(10081f);
//            employee.setDeptId(4);
            employee.setName("王老六");
            sqlSession.insert("EMPLOYEE.addEmp",employee);
            System.out.println(employee.getId());
            sqlSession.commit();
        } catch (Exception e) {
            sqlSession.rollback();
            throw new RuntimeException(e);
        }
    }

    @Test
    public void updateDept() {
        try {
            Dept dept = new Dept();
            dept.setId(14);
            dept.setName("运营部");
            sqlSession.update("DEPT.updateDept",dept);
            sqlSession.commit();
        } catch (Exception e) {
            sqlSession.rollback();
            throw new RuntimeException(e);
        }
    }

    @Test
    public void updateEmployee() {
        try {
            Employee employee = new Employee();
            employee.setHiredate(new Date());
            employee.setId(6);
            employee.setSalary(15000f);
//            employee.setDeptId(2);
            employee.setName("老八");
            sqlSession.update("EMPLOYEE.updateEmp",employee);
            sqlSession.commit();
        } catch (Exception e) {
            sqlSession.rollback();
            throw new RuntimeException(e);
        }
    }

    @Test
    public void deleteDept() {
        try {
            sqlSession.delete("DEPT.deleteDept",16);
            sqlSession.commit();
        }catch (Exception e) {
            sqlSession.rollback();
            throw new RuntimeException(e);
        }
    }

    @Test
    public void getEmpList() {

        Employee employee1 = new Employee();
//        employee1.setId(4);
//        employee1.setName("张三");
        List<Employee> employees = sqlSession.selectList("EMPLOYEE.getEmpList",employee1);
        for (Employee employee : employees) {
            System.out.println(employee);

        }

    }

    @Test
    public void selectEmpWithDept() {
        List<Employee> employees = sqlSession.selectList("EMPLOYEE.selectEmpWithDept");
        for (Employee emp : employees) {
            System.out.println(emp);
        }
    }

    @Test
    public void selectDeptWithEmp() {
        List<Dept> depts = sqlSession.selectList("DEPT.getDeptWithEmp");

        for (Dept dept : depts) {
            System.out.println(dept.getId());
            System.out.println(dept.getName());

            if (dept.getEmployees() != null) {
                for (Employee emp : dept.getEmployees()) {
                    System.out.println(emp);
                }
            }
            System.out.println("====================================");
        }
    }

    @Test
    public void updateEmpWithLabel() {

        Employee employee = new Employee();
        employee.setId(7);
        employee.setSalary(2200f);
        try{
            sqlSession.update("EMPLOYEE.updateEmpWithLabel",employee);
            sqlSession.commit();
        }catch (Exception e){
            sqlSession.rollback();
            throw new RuntimeException(e);
        }
    }

    @Test
    public void selectEmpWithChooseWhen() {
//        Employee employee = new Employee();
//        employee.setId(5);
//        employee.setName("张三");
        ArrayList<Integer> list = new ArrayList<Integer>();
        list.add(1);
        list.add(2);
        list.add(3);


        List<Employee> empList = sqlSession.selectList("EMPLOYEE.selectEmpByIds", list);
        for (Employee emp : empList) {
            System.out.println(emp);
        }

    }

}
